// todo============= 顶部Tab栏=========================
async function top_render() {
    const res = await axios.get('/index/index')
    // console.log(res);
    const newData = res.data.data.allCate
    // console.log(newData);
    document.querySelector('.container ul').innerHTML = newData.map((item) => {
        return `  
        <li class="menu-item menu-item-type-taxonomy menu-item-object-category ">
            <a title="" href="index.html"><span class="fa ${item.icon}"></span>${item.catename}</a>
        </li>
        `
    }).join('')
}
top_render()


//todo =================== 修改用户资料 ==========================
async function User_data_render() {
    const res = await axios.get('/user/update')
    console.log(res);
    // console.log(res.data.data.allLink);2
    // const newData = res.data.data.allLink
    // console.log(newData);
    // document.querySelector('.footer .link').innerHTML = newData.map((item) => {
    //     return ` 
    //     <a href="" title="obj.des">${item.des}</a> <span>|</span>
    //     `
    // }).join('') 
}
User_data_render()


// //todo ================ 底部 友情链接  =======================
async function bottom_render() {
    const res = await axios.get('/index/index')
    // console.log(res);
    // console.log(res.data.data.allLink);
    const newData = res.data.data.allLink
    // console.log(newData);
    document.querySelector('.footer .link').innerHTML = newData.map((item) => {
        return ` 
        <a href="" title="obj.des">${item.des}</a> <span>|</span>
        `
    }).join('')
}
bottom_render()


// //todo ================ 底部 版权 ============================
async function copyright_render() {
    const res = await axios.get('/index/index')
    // console.log(res);
    const newData = res.data.data.config.allData
    // console.log(newData);
    newData.forEach((item) => {
        if (item.id === 55) {
            document.querySelector('.footer p').innerHTML = ` <p>&copy; 2021 <a href="http://blog.weknow.cn">优点达资讯,</a>
            &nbsp;&nbsp;联系邮箱：${item.attr_value}
            &nbsp;&nbsp; <a href="">备案号信息</a>
            </p>`
            // document.querySelector('.footer p').innerHTML = newData.map((item) => {
            //     return `
            // <p>&copy; 2021 <a href="http://blog.weknow.cn">优点达资讯,</a>
            // &nbsp;&nbsp;联系邮箱：${item.attr_value}
            // &nbsp;&nbsp; <a href="">备案号信息</a>
            // </p>
            //     `
            // }).join('')
        }
    });
}
copyright_render()


const laydate = layui.laydate;

render()
async function render() {
  if (localStorage.getItem('token')) {
    // 拿取注册id
    const userInfo1 = JSON.parse(localStorage.getItem('userInfo'))

    // 发送请求
    const res = await axios.get('http://124.223.14.236:8060/user/getUserInfo')


    // 解构数据
    const { data: { data: { userInfo } } } = res
    const { address, birthday, default_data, icon, id, mobile, notes, password, sex, username } = userInfo


    //  判断用户是否上传头像
    let images = './static/images/default.png'
    if (icon) {
      images = icon
    }
    $('.user-icon img').attr('src', `http://124.223.14.236:8060/${images}`)


    // 将用户个人信息填入

    $('.name').text(`昵称: ${username}`)
    $('.declaration').text(`个性宣言: ${notes}`)
    // 性别判断
    const sex1 = sex === 0 ? '男' : '女'
    $('.sex').text(`性别: ${sex1}`)

    $('.cellPhoneNumber').text(`手机号: ${mobile}`)
    $('.birthday').text(`出生年月: ${birthday}`)
  } else {
    // 清空内容
    $('.name').text(`昵称:`)
    $('.declaration').text(`个性宣言:`)
    $('.sex').text(`性别: `)
    $('.cellPhoneNumber').text(`手机号:`)
    $('.birthday').text(`出生年月:`)
    // 更换为登录和注册
    $('.slinks .user1').hide().siblings('a').show()
    $('.nickname').hide()
    $('.user-icon img').attr('src', `./static/images/default.png`)
  }
}

// 点击图片 默认点击上传按钮
$('.file').on('click', function () {
  $('#file').click()
})

// 展示用户名
if (localStorage.getItem('token')) {
  $('.slinks .user1').show().siblings('a').hide()
  $('.nickname').show().html('退出')
}


// 退出
$('.nickname').click(function () {
  layer.msg('已登出', {
    icon: 1,
    time: 500 //2秒关闭（如果不配置，默认是3秒）
  }, function () {
    localStorage.removeItem('userInfo')
    localStorage.removeItem('token')
    render()
  });
})

// todo ===================== 顶部 登入注册功能 ==============================
async function getuser() {
  const {data:{data}} = await axios.get('user/getUserInfo', {
      headers: { token: localStorage.getItem('token')}
  })
  console.log(data);
  // 账号信息
  document.querySelector('.slinks').innerHTML = `
   <i class="iconfont icon-yonghu"></i>你好${data.userInfo.username} 
  <i class="iconfont icon-exit-full"></i>
  `
}
getuser()